package cn.zxh.csdn_maven.work.flow.download;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.HttpClient;
import org.apache.log4j.Logger;


/**
 * 
 * @author zhangxinhua
 *
 */
public abstract class AbstractFlowController implements IFlowController{

	private Logger logger=Logger.getLogger(AbstractFlowController.class);
	private Map<String, Object> result=new HashMap<String, Object>();
	private IFlowController flow;
	
	/**自由的抽象方法*/
	public abstract Map<String, Object> execute(HttpClient client,Map<String, Object> params) throws Exception;
	public abstract String getBaseUrl();
	
	
	public IFlowController setNextFlow(IFlowController flow) {
		this.flow=flow;
		return this.flow;
	}

	public Map<String, Object> run(HttpClient client, Map<String, Object> params)
			throws Exception {
		result.putAll(execute(client, params));
		if(result!=null && result.get("msg")!=null&&"success".equals(result.get("msg"))){
			logger.info(getBaseUrl()+"页面已经获取");
			if(flow==null){
				logger.info(this.getClass().getSimpleName()+"已经是最后的工作链了");
			}else {
				result.putAll(this.flow.run(client,params));
			}
		}else {
			logger.info(getBaseUrl()+"获取下一步地址出错");
		}
		return result;
	}
	
}
